package com.example.week04_work2;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.LockSupport;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author nhsoft.ll
 */
public class ParkDemo {


    public static void main(String[] args) throws Exception {

        ParkDemoSumTask sumTask = new ParkDemoSumTask(Thread.currentThread());

        long start = System.currentTimeMillis();
        sumTask.start();

        LockSupport.park();

        System.out.println("异步计算结果为：" + sumTask.getResult());
        System.out.println("使用时间：" + (System.currentTimeMillis() - start) + " ms");

    }
}

class ParkDemoSumTask extends Thread {

    private Thread mainThread;

    public ParkDemoSumTask(Thread thread){
        this.mainThread = thread;
    }

    private int result;

    public int getResult() {
        return result;
    }

    @Override
    public void run() {
        result = sum();
        System.out.println("线程运行结果:" + result);
        LockSupport.unpark(mainThread);
    }

    private int sum() {
        return fibo(36);
    }

    private int fibo(int a) {
        if (a < 2)
            return 1;
        return fibo(a - 1) + fibo(a - 2);
    }
}


